<template>
  <div class="wrap">
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      status-icon
      :rules="rules"
      label-width="auto"
      class="ruleForm-wrap"
    >
      <el-form-item label="用户名" prop="user_wanted">
        <el-input
          v-model="ruleForm.user_wanted"
          message="用户名"
          :disabled="true"
          placeholder
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nick_name">
        <el-input v-model="ruleForm.nick_name" message="修改昵称" />
      </el-form-item>
      <el-form-item label="新密码" prop="user_pwd" show-overflow-tooltip>
        <el-input v-model="ruleForm.user_pwd" message="修改密码" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model.number="ruleForm.mobile" message="修改手机号" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="ruleForm.email" message="修改邮箱" />
      </el-form-item>
      <el-form-item label="登录有效期">
        <el-select v-model="ruleForm.token_expired" placeholder="请选择">
          <el-option label="永不失效" value="0" />
          <el-option label="10分钟" value="10" />
          <el-option label="30分钟" value="30" />
          <el-option label="60分钟" value="60" />
        </el-select>
      </el-form-item>
      <el-form-item class="btn-wrap">
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          修改
        </el-button>
        <el-button type="warning" plain @click="resetForm(ruleFormRef)"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { postData } from '@/http/request.js'
import { useRouter, useRoute } from 'vue-router'
let router = useRouter()
let route = useRoute()

let ruleFormRef = ref(null)
const user_name = localStorage.getItem('username')

var checkPass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    callback()
  }
}
var checkEmail = (rule, value, callback) => {
  const regEmail = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/
  if (regEmail.test(value)) {
    return callback()
  } else if (value == '') {
    return callback()
  } else {
    callback(new Error('请输入合法邮箱'))
  }
}
var checkPhone = (rule, value, callback) => {
  const phoneReg = /^1[3|4|5|7|8][0-9]{9}$/
  if (!Number.isInteger(+value)) {
    callback(new Error('请输入数字值'))
  } else {
    if (phoneReg.test(value)) {
      callback()
    } else if (value == '') {
      return callback()
    } else {
      callback(new Error('电话号码格式不正确'))
    }
  }
}

const ruleForm = reactive({
  user_wanted: user_name,
  nick_name: localStorage.getItem('nick_name'),
  modifyPwd: '',
  mobile: localStorage.getItem('mobile'),
  email: localStorage.getItem('email'),
  token_expired: localStorage.getItem('token_expired'),
})

const rules = reactive({
  nick_name: [{ required: false, message: '请输入新的昵称', trigger: 'blur' }],
  modifyPwd: [
    {
      required: false,
      message: '请输入新的密码',
      trigger: 'change',
    },
  ],
  mobile: [
    { required: false, message: '请输入手机号码', trigger: 'blur' },
    { validator: checkPhone, trigger: 'blur' },
  ],
  email: [
    { required: false, message: '请输入邮箱', trigger: 'blur' },
    { validator: checkEmail, trigger: 'blur' },
  ],
  token_expired: [{ required: false, message: '', trigger: 'blur' }],
})

const submitForm = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      postData('/sys/admin/update', {
        user_wanted: ruleForm.user_wanted,
        user_pwd: ruleForm.user_pwd,
        nick_name: ruleForm.nick_name,
        mobile: ruleForm.mobile,
        email: ruleForm.email,
        token_expired: Number(ruleForm.token_expired),
      })
        .then((res) => {
          if (res.code == 0) {
            ElMessage({
              type: 'success',
              message: '修改成功',
            })
            localStorage.setItem(
              'nick_name',
              ruleForm.nick_name
            )
            localStorage.setItem('mobile', ruleForm.mobile)
            localStorage.setItem('email', ruleForm.email)
            localStorage.setItem(
              'token_expired',
              ruleForm.token_expired
            )
          } else {
            ElMessage({
              type: 'error',
              message: '修改失败',
            })
          }
        })
        .catch((err) => {
          console.log(err)
        })
    } else {
      ElMessage({
        type: 'error',
        message: fields,
      })
    }
  })
}

const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
}

onMounted(() => {})
</script>
<style scope>
.wrap {
  margin: 20px 0 0 20px;
}

.title {
  width: 100%;
  height: 40px;
  line-height: 40px;
  position: relative;
  /* background: #c0dee2; */
  padding-left: 30px;
  font-size: 14px;
  margin: 6px 0 20px 0;
  /* border-radius: 12px 12px 0 0; */
  /* font-weight: bold; */
}

.title::before {
  content: '';
  width: 2px;
  height: 16px;
  background: #676a6d;
  position: absolute;
  top: 12px;
  left: 15px;
}

.title span {
  float: left;
  color: #676a6d;
  margin-left: 25px;
}

.btn-wrap {
  margin-top: 30px;
}
</style>
<style>
.box-item {
  width: 200px !important;
  white-space: pre-wrap;
}
</style>
